/* 
 * Advanced Foundation Classes
 * Copyright (C) 2000/2004  Fabio Rotondo - Andrea Galimberti
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "entry.h"

static const char class_name [] = "EGUI Widget Entry";

static int afc_easygui_internal_entry_get_template ( DynamicClass * dyn );
static int afc_easygui_internal_cb_entry ( DynamicClass * dyn );

// EVENTS
static void afc_easygui_entry_event_activate ( GtkWidget * w, gpointer data );
static void afc_easygui_entry_event_insert_text ( GtkWidget * editable, gchar * new_text, gint new_text_length, gint * position, gpointer data );

// METHODS
static int afc_easygui_entry_method_get_attr ( DynamicClass * dc );
static int afc_easygui_entry_method_set_attr ( DynamicClass * dc );
static int afc_easygui_entry_method_get_value ( DynamicClass * dc );
static int afc_easygui_entry_method_set_value ( DynamicClass * dc );

DynamicClass * afc_easygui_internal_entry_new_instance ()
{
	DynamicClass * dyn = NULL;

	if ( ( dyn = afc_dynamic_class_new () ) == NULL ) 
	{
		AFC_LOG_FAST ( AFC_ERR_NO_MEMORY );
		return ( NULL );
	}

	afc_dynamic_class_add_method ( dyn, "get_template", NULL,  afc_easygui_internal_entry_get_template );
	afc_dynamic_class_add_method ( dyn, "open_callback", NULL,  afc_easygui_internal_cb_entry );
	afc_dynamic_class_add_method ( dyn, "enabled",	NULL, 	afc_easygui_widget_method_enabled );

	afc_dynamic_class_add_method ( dyn, "get_attr", NULL,  afc_easygui_entry_method_get_attr );
	afc_dynamic_class_add_method ( dyn, "set_attr", NULL,  afc_easygui_entry_method_set_attr );
	afc_dynamic_class_add_method ( dyn, "get_value", NULL,  afc_easygui_entry_method_get_value );
	afc_dynamic_class_add_method ( dyn, "set_value", NULL,  afc_easygui_entry_method_set_value );

	return ( dyn );
}

int afc_easygui_internal_entry_del_instance ( DynamicClass * dyn )
{
	return ( afc_easygui_widget_standard_free ( dyn ) );
	/*
	if ( dyn != NULL ) afc_dynamic_class_delete ( dyn );

	return ( AFC_ERR_NO_ERROR );
	*/
}

static int afc_easygui_internal_entry_get_template ( DynamicClass * dyn )
{
	dyn->result = "MAXLEN ACTIVATED INSERTED FIXED/S PASSWORD/S";
	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_internal_cb_entry ( DynamicClass * dyn )
{
	EasyGUI * egui = dyn->info;
	EasyGUIContext * context = egui->curr_context;
	GtkWidget * w;
	char * maxlen = NULL;
	char * activated, * inserted;
	int maxnum = 0;
	short password;
	short fixed;
	int afc_res = AFC_ERR_NO_ERROR;
	DynamicClass * dc;
	guint sig_id = 0;

	if ( context->curr_container == NULL ) return ( AFC_EASYGUI_ERR_NO_CONTAINER );

	afc_easygui_widget_parse_default_params ( egui );

	activated 	= afc_cmd_parser_arg_get_by_name ( egui->parser, "ACTIVATED" );
	inserted 	= afc_cmd_parser_arg_get_by_name ( egui->parser, "INSERTED" );

	fixed   = ( short ) ( int ) afc_cmd_parser_arg_get_by_name ( egui->parser, "FIXED" );
	password = ( short ) ( int ) afc_cmd_parser_arg_get_by_name ( egui->parser, "PASSWORD" );

	if ( ( maxlen = afc_cmd_parser_arg_get_by_name ( egui->parser, "MAXLEN") ) == NULL ) w = gtk_entry_new ();
	else {
		maxnum = atoi ( maxlen );
		w = gtk_entry_new_with_max_length ( maxnum );
	}

	if ( ( dc = afc_dynamic_class_master_new_instance ( egui->commands, "entry" ) ) == NULL )
		return ( AFC_LOG_FAST_INFO ( AFC_ERR_NO_MEMORY, "dynclass instance" ) );

	WIDGET_SET_CONTEXT ( dc, context );

	/* first try to add the widget to the current container, then, if successful, set the other attributes */
	if ( ( afc_res = afc_easygui_internal_add_widget ( context, w, dc ) ) != AFC_ERR_NO_ERROR ) return ( afc_res );

	if ( ( afc_res = afc_easygui_widget_connect_signal ( dc,  w, "activate", activated, NULL, (GtkSignalFunc) afc_easygui_entry_event_activate ) ) != AFC_ERR_NO_ERROR )
		return ( afc_res );

	// Connects the "insert_text" handler (if present)
	if ( ( afc_res = afc_easygui_widget_connect_signal ( dc, w, "insert_text", inserted, &sig_id, (GtkSignalFunc) afc_easygui_entry_event_insert_text ) ) != AFC_ERR_NO_ERROR )
		return ( afc_res );

	afc_dynamic_class_set_var ( dc, AFC_DYNAMIC_CLASS_VAR_KIND_NUM, "insert_text_id", ( void * ) sig_id );

	gtk_entry_set_editable ( GTK_ENTRY ( w ), !fixed );
	gtk_entry_set_visibility ( GTK_ENTRY ( w ), !password );

	return ( afc_res );
}

// EVENTS
static void afc_easygui_entry_event_activate ( GtkWidget * w, gpointer data )
{
	DynamicClass * dc = data;
	EasyGUIEntrySignalFunc sig;
	const char * text;

	// Get the function pointer (forcing it to be a EasyGUIComboSignal)
	// If the function pointer does not exists, this macro will just return
	WIDGET_GET_CALLBACK_CAST ( dc, "activate", sig, EasyGUIEntrySignalFunc );

	text = gtk_entry_get_text ( GTK_ENTRY ( w ) );

	sig ( dc, WIDGET_GET_CONTEXT ( dc ), text );
}

static void afc_easygui_entry_event_insert_text ( GtkWidget * editable, gchar * new_text, gint new_text_length, gint * position, gpointer data )
{
	DynamicClass * dc = data;
	EasyGUIEntryInsertTextSignalFunc sig;
	char * str;
	guint sig_id;

	// Get the function pointer (forcing it to be a EasyGUIComboSignal)
	// If the function pointer does not exists, this macro will just return
	WIDGET_GET_CALLBACK_CAST ( dc, "insert_text", sig, EasyGUIEntryInsertTextSignalFunc );

	sig_id = ( guint ) afc_dynamic_class_get_var ( dc, "insert_text_id" );

	// Blocks the signal propagation
	g_signal_handler_block ( G_OBJECT ( editable ), sig_id );

	if ( ( str = afc_string_dup ( new_text ) ) != NULL )
	{
		if ( sig ( dc, WIDGET_GET_CONTEXT ( dc ), str, new_text_length ) )
		{
			if ( afc_string_len ( str ) ) gtk_editable_insert_text ( GTK_EDITABLE ( editable ) , str, afc_string_len ( str ), position);
		}

		afc_string_delete ( str );
	} else 
		AFC_LOG_FAST ( AFC_ERR_NO_MEMORY );

	g_signal_handler_unblock ( G_OBJECT ( editable ), sig_id );
        gtk_signal_emit_stop_by_name ( GTK_OBJECT ( editable ), "insert_text" );
}

// METHODS

// Gets an attr from the widget
static int afc_easygui_entry_method_get_value ( DynamicClass * dc )
{
	EasyGUIWidgetData * ewidget = afc_dynamic_class_get_var ( dc, "ewidget" );

	dc->result = ( void * ) gtk_entry_get_text ( GTK_ENTRY ( ewidget->gtk_handle ) );		

	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_entry_method_set_value ( DynamicClass * dc )
{
	char * buffer;
	EasyGUIWidgetData * ewidget = afc_dynamic_class_get_var ( dc, "ewidget" );

	buffer 	= ( char * ) afc_array_master_first ( dc->args );

	gtk_entry_set_text ( GTK_ENTRY ( ewidget->gtk_handle ), ( const char * ) buffer );		

	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_entry_method_get_attr ( DynamicClass * dc )
{
	char * * buffer;
	int	 attr;
	EasyGUIWidgetData * ewidget = afc_dynamic_class_get_var ( dc, "ewidget" );

	attr 	= ( int ) afc_array_master_first ( dc->args );
	buffer 	= ( char * * ) afc_array_master_next ( dc->args );

	// fprintf ( stderr, "buf: %x\n", ( int ) buffer );

	switch ( attr )
	{
		case AFC_EASYGUI_WIDGET_ATTR_VALUE:
			dc->result = ( void * ) gtk_entry_get_text ( GTK_ENTRY ( ewidget->gtk_handle ) );		
			*buffer = ( char * ) dc->result;
			break;

		case AFC_EASYGUI_WIDGET_ATTR_NAME:
			dc->result = ( void * ) gtk_widget_get_name ( GTK_WIDGET ( ewidget->gtk_handle ) );
			*buffer = ( char * ) dc->result;
			break;

		default:
			fprintf ( stderr, "ENTRY: unsupported attr\n" );
	}

	return ( AFC_ERR_NO_ERROR );
}

// Sets an attr value to the widget
static int afc_easygui_entry_method_set_attr ( DynamicClass * dc )
{
	char * buffer;
	int	 attr;
	EasyGUIWidgetData * ewidget = afc_dynamic_class_get_var ( dc, "ewidget" );

	attr 	= ( int ) afc_array_master_first ( dc->args );
	buffer 	= ( char * ) afc_array_master_next ( dc->args );

	switch ( attr )
	{
		case AFC_EASYGUI_WIDGET_ATTR_VALUE:
			gtk_entry_set_text ( GTK_ENTRY ( ewidget->gtk_handle ), ( const char * ) buffer );		
			break;

		case AFC_EASYGUI_WIDGET_ATTR_VISIBLE:
			g_object_set ( G_OBJECT ( ewidget->gtk_handle ), "visible", ( gboolean ) ( int ) buffer, NULL, NULL );
			break;

		case AFC_EASYGUI_WIDGET_ATTR_ACTIVE:
			g_object_set ( G_OBJECT ( ewidget->gtk_handle ), "sensitive", ( gboolean ) ( int ) buffer, NULL, NULL );
			break;

		case AFC_EASYGUI_WIDGET_ATTR_FOCUS:
			gtk_widget_grab_focus ( GTK_WIDGET ( ewidget->gtk_handle ) );
			break;

		case AFC_EASYGUI_WIDGET_ATTR_NAME:
			gtk_widget_set_name ( GTK_WIDGET ( ewidget->gtk_handle ), ( const char * ) buffer );
			break;

		default:
			fprintf ( stderr, "ENTRY: unsupported attr\n" );
	}

	return ( AFC_ERR_NO_ERROR );
}
